﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using HNG.LIB.Controls;
using HNG.Portal.BLL;
using HNG.Portal.DAL;
using HNG.Portal.DAL.EntityClasses;
using HNG.Portal.DAL.FactoryClasses;
using HNG.Portal.DAL.HelperClasses;
using SD.LLBLGen.Pro.ORMSupportClasses;
using System.Web.UI.HtmlControls;
using System.Data;
using System.IO;
public partial class AdminPortal_Controls_Media_CategoryFile_Info : System.Web.UI.UserControl
{
    TblMediaCategoryManager _TblMediaCategoryManager = new TblMediaCategoryManager();
    public int MeID = 0;
    protected void Page_Load(object sender, EventArgs e)
    {
        if (!IsPostBack)
        {
            try { MeID = Convert.ToInt32(Request.QueryString["id"].Trim()); }
            catch { MeID = 0; }

            loadCateParent();

            //FunctionExtend.LoadViTri(ddlCatePoi);

            if (MeID == 0) //Add cate
            {
                lblTitle.Text = "Thêm danh mục file";
                Toolbox1.ShowDel = false;
            }
            else //sua cate
            {
                imgbtn_Delete.Enabled = true;
                lblDelete.Visible = true;
                imgbtn_Delete.ImageUrl = "~/AdminPortal/images/icon/remove_16.gif";
                lblTitle.Text = "Sửa danh mục file: " + MeID;

                BindData(MeID);
            }
        }
    }

    protected void BindData(int ID)
    {
        TblMediaCategoryEntity _TblMediaCategoryEntity = _TblMediaCategoryManager.SelectOne(ID);
        if (_TblMediaCategoryEntity == null) { lblthongbao.Text = "<img src='../../Images/validatebox_warning.png' alt='Cảnh báo' width='16' height='16' /> <span style='font-size:11px;color:red;'>  Không có danh mục này </span>"; return; }
        hidID.Value = ID.ToString();
        txtID.Text = ID.ToString();
        txtNameCate.Text = _TblMediaCategoryEntity.TenDanhMuc;
        txtOrder.Text = _TblMediaCategoryEntity.ThuTu.ToString();
        string dmcha = _TblMediaCategoryEntity.DanhMucCha.ToString();
        ddlCateParent.SelectedValue = dmcha;
        //ddlCatePoi.SelectedValue = _TblMediaCategoryEntity.ViTri;
        img_news.Visible = true;
        img_news.ImageUrl = (_TblMediaCategoryEntity.Icon != "") ? _TblMediaCategoryEntity.Icon : "~/AdminPortal/Images/icon/no_image.jpg";
        if (string.IsNullOrEmpty(_TblMediaCategoryEntity.Icon))
        {
            imgbtn_Delete.Enabled = false;
            imgbtn_Delete.ImageUrl = "~/AdminPortal/images/icon/remove_16_hiden.gif";
            img_news.Visible = false;
        }
        else
        {
            img_news.Visible = true;
        }
        //chkIsHome.Checked = Convert.ToBoolean(_TblMediaCategoryEntity.IsHome);
        chkStatus.Checked = Convert.ToBoolean(_TblMediaCategoryEntity.TrangThai);
    }

    private void loadCateParent()
    {
        DataTable _dtDM = new DataTable();
        try
        {
            TblMediaCategoryEntity _TblMediaCategoryEntity = new TblMediaCategoryEntity();
            _TblMediaCategoryEntity.LangId = HNG.LIB.Localization.LocalizationUtils.GetCurrentLanguage();
            _TblMediaCategoryEntity.Type = "category";
            _dtDM = _TblMediaCategoryManager.be_SelectToDropdownlist(_TblMediaCategoryEntity);
        }
        catch { _dtDM = null; }

        if (_dtDM != null || _dtDM.Rows.Count > 0)
            HNG.LIB.BIND_SOURCE.Bind_DropDownListV3(ddlCateParent, _dtDM, TblDanhMucSanPhamFields.TenDanhMuc.Name, TblDanhMucSanPhamFields.Id.Name, TblDanhMucSanPhamFields.DanhMucCha.Name);
    }   

    protected void btnSave_Click(object sender, EventArgs e)
    {
        BindToData();
        Response.Redirect(SiteMap.CurrentNode.ParentNode.Url);
    }
    protected void btnDel_Click(object sender, EventArgs e)
    {
        if (!string.IsNullOrEmpty(hidID.Value))
        {
            TblMediaCategoryEntity _TblMediaCategoryEntity = _TblMediaCategoryManager.SelectOne(Convert.ToInt32(hidID.Value));
            if (_TblMediaCategoryEntity != null)
            {
                if (!string.IsNullOrEmpty(_TblMediaCategoryEntity.Icon))
                {
                    string ImageDefault = Server.MapPath(_TblMediaCategoryEntity.Icon);
                    if (File.Exists(ImageDefault)) File.Delete(ImageDefault);
                }
            }
            _TblMediaCategoryManager.Delete(Convert.ToInt32(hidID.Value));
        }
        Response.Redirect(SiteMap.CurrentNode.ParentNode.Url);
    }
    protected void btnApply_Click(object sender, EventArgs e)
    {
        BindToData();
        Response.Redirect(ResolveUrl("~/AdminPortal/Pages/FlieCategoryEdit.aspx?id=") + hidID.Value);
        //BindData(Convert.ToInt32(hidID.Value));
    }

    private void BindToData()
    {
        if (txtNameCate.Text == "") { lblthongbao.Text = "<img src='../../Images/icon/Icon_warning.gif' alt='Cảnh báo' /> <span style='font-size:11px;color:red;'>  Nhập tên danh mục !</span>"; txtNameCate.Focus(); return; }
        TblMediaCategoryEntity _TblMediaCategoryEntity = new TblMediaCategoryEntity();
        if (!string.IsNullOrEmpty(hidID.Value)) _TblMediaCategoryEntity.Id = Convert.ToInt32(hidID.Value);//update
        _TblMediaCategoryEntity.LangId = HNG.LIB.Localization.LocalizationUtils.GetCurrentLanguage();
        _TblMediaCategoryEntity.TenDanhMuc = txtNameCate.Text;
        _TblMediaCategoryEntity.DanhMucCha = Convert.ToInt32(ddlCateParent.SelectedValue);
        _TblMediaCategoryEntity.TenDanhMucCha = ddlCateParent.SelectedItem.Text;
        //_TblMediaCategoryEntity.ViTri = ddlCatePoi.SelectedValue;
        _TblMediaCategoryEntity.ThuTu = HNG.LIB.Common.CheckErr.CheckInputValidate(txtOrder.Text, 1);
        _TblMediaCategoryEntity.TrangThai = (chkStatus.Checked == true) ? true : false;
        _TblMediaCategoryEntity.Type = "category";
        //_TblMediaCategoryEntity.IsHome = (chkIsHome.Checked == true) ? true : false;
        if (FileUpload1.FilePath != "")
        {
            string ImageDefault = Server.MapPath(_TblMediaCategoryEntity.Icon);
            if (File.Exists(ImageDefault)) File.Delete(ImageDefault);
            _TblMediaCategoryEntity.Icon = FileUpload1.FilePath;
        }

        if (!string.IsNullOrEmpty(hidID.Value))
            _TblMediaCategoryManager.Update(_TblMediaCategoryEntity);//update
        else
        {
            _TblMediaCategoryManager.Insert(_TblMediaCategoryEntity);//Insert
            hidID.Value = _TblMediaCategoryEntity.Id.ToString();
        }

    }
    protected void btnBack_Click(object sender, EventArgs e)
    {
        Response.Redirect(SiteMap.CurrentNode.ParentNode.Url);
    }
    protected void imgbtn_Delete_Click(object sender, ImageClickEventArgs e)
    {
        if (!string.IsNullOrEmpty(hidID.Value))
        {
            TblMediaCategoryEntity _TblMediaCategoryEntity = _TblMediaCategoryManager.SelectOne(Convert.ToInt32(hidID.Value));
            if (_TblMediaCategoryEntity != null)
            {
                if (!string.IsNullOrEmpty(_TblMediaCategoryEntity.Icon))
                {
                    string defaultImage = Server.MapPath(_TblMediaCategoryEntity.Icon);
                    if (File.Exists(defaultImage)) File.Delete(defaultImage);
                    _TblMediaCategoryEntity.Icon = "";
                    _TblMediaCategoryManager.Update(_TblMediaCategoryEntity);
                }
            }
        }
        BindData(MeID);
    }
}